/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.manchester.biology.eqhei.implementations;

import edu.manchester.biology.eqhei.interfaces.eQHEI_Attribute;
import edu.manchester.biology.eqhei.interfaces.eQHEI_AttributeSet;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Michael Kendall
 */
public class eQHEI_AttributeSetIMP implements eQHEI_AttributeSet{
    private ArrayList<eQHEI_Attribute> attributes = new ArrayList<>();
    private int attributeIndex = -1;
    private String name = "";
    
    public eQHEI_AttributeSetIMP(){
        
    }
    
    public eQHEI_AttributeSetIMP(String n){
        name = n;
    }

    @Override
    public eQHEI_Attribute next() throws Exception {
        eQHEI_Attribute rvalue;
        if(attributeIndex >= attributes.size()-1){
            attributeIndex = attributes.size();
            throw new Exception("Attribute index out of bounds on next");
        }
        ++attributeIndex;
        rvalue = attributes.get(attributeIndex);
        return rvalue;
    }

    @Override
    public eQHEI_Attribute previous() throws Exception {
        eQHEI_Attribute rvalue;
        if(attributeIndex == 0){
            --attributeIndex;
            throw new Exception("Attribute index out of bounds on previous");
        }
        --attributeIndex;
        rvalue = attributes.get(attributeIndex);
        return rvalue;
    }

    @Override
    public void add(eQHEI_Attribute attribute) {
        attributes.add(attribute);
    }

    @Override
    public eQHEI_Attribute get(String name) throws Exception {
        return attributes.get(find(name));
    }

    @Override
    public eQHEI_Attribute get(Integer i) throws Exception {
        return attributes.get(i);
    }

    @Override
    public Integer length() {
        return attributes.size();
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public eQHEI_Attribute remove(String name) throws Exception {
        return attributes.remove(find(name));
    }
    
    private int find(String name) throws Exception{
        eQHEI_Attribute a[] = new eQHEI_Attribute[attributes.size()];
        attributes.toArray(a);
        int i = 0;
        while(i<a.length && !a[i].getName().equalsIgnoreCase(name)){
            ++i;
        }
        if(i<a.length){
            return i;
        }
        else{
            throw new Exception(name + " Not Found");
        }
    }
   
}
